package com.bxait;

import com.bxait.utils.ESClientSingle;
import com.bxait.utils.User;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;
import java.util.Map;

/**
 * @author Chen
 * @description: es查询数据
 * @ClassName ESTest_Client
 * @create 2023-04-08 16:16
 * @Version 1.0
 */
public class ESTest_Doc_Query {

    public static void main(String[] args) throws IOException {
        // 创建ES的客户端
        RestHighLevelClient client = ESClientSingle.getInstant();

        // 查询数据
        SearchRequest request = new SearchRequest();
        // 指定索引
        request.indices("user");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        // 排序
//        builder.sort("age", SortOrder.DESC);

        // 选择字段查询
//        String[] excludes = {"name"}; // 排除
//        String[] includes = {};       // 包含
//        builder.fetchSource(includes,excludes);

        // (当前页面-1) * 每页显示的条数  分页
//        builder.from(0);
//        builder.size(2);

        // 组合查询
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        // 年龄必须是30,性别必须是男
//        boolQueryBuilder.must(QueryBuilders.matchQuery("age","30"));
//        boolQueryBuilder.must(QueryBuilders.matchQuery("sex","男"));

        // 年龄必须是30,性别必须不是男
//        boolQueryBuilder.must(QueryBuilders.matchQuery("age","30"));
//        boolQueryBuilder.mustNot(QueryBuilders.matchQuery("sex","男"));

        // 年龄可以是20也可以是30
//        boolQueryBuilder.should(QueryBuilders.matchQuery("age","30"));
//        boolQueryBuilder.should(QueryBuilders.matchQuery("age","20"));
//        builder.query(boolQueryBuilder);

        // 范围查询
//        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age");
//        // 大于等于30,小于等于40
//        rangeQueryBuilder.gte(30);
//        rangeQueryBuilder.lte(40);
//        builder.query(rangeQueryBuilder);

        // 模糊查询
//        FuzzyQueryBuilder fuzzyQuery = QueryBuilders.fuzzyQuery("name","chen");
//        // 标识差一个字符也能查出来   fuzziness表示偏差的一个距离
//        fuzzyQuery.fuzziness(Fuzziness.ONE);
//        builder.query(fuzzyQuery);

        // 高亮查询
//        TermQueryBuilder termQuery = QueryBuilders.termQuery("name", "chen");
//
//        HighlightBuilder highlightBuilder = new HighlightBuilder();
//        // 前置标签
//        highlightBuilder.preTags("<font color='red'>");
//        highlightBuilder.postTags("</font>");
//        highlightBuilder.field("name");
//        // 高亮
//        builder.highlighter(highlightBuilder);
//        builder.query(termQuery);

        // 聚合查询
        // 对年龄取最大值
//        AggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
//        builder.aggregation(aggregationBuilder);

        // 分组查询
        TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("ageGroup").field("age");
        builder.aggregation(termsAggregationBuilder);


        // 条件查询
//        TermQueryBuilder queryBuilder = QueryBuilders.termQuery("age", 20);
//        builder.query(queryBuilder);
        // 全量查询
//        builder.query(QueryBuilders.matchAllQuery());

        request.source(builder);
        SearchResponse response
                = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(response.getTook());
        Aggregations aggregations = response.getAggregations();
        System.out.println(new ObjectMapper().writeValueAsString(aggregations));

        System.out.println("--------------------------------");

        for (SearchHit hit : hits) {
//            System.out.println(hit.getHighlightFields());
            System.out.println(hit.getSourceAsString());
        }

        // 关闭es客户端
        client.close();
    }

}
